#!/bin/sh

PREREQ="mdadm mdrun multipath"

prereqs()
{
	echo "$PREREQ"
}

case $1 in
# get pre-requisites
prereqs)
	prereqs
	exit 0
	;;
esac

activate_vg()
{
	local dev="$1"

	# Make sure that we have a non-empty argument
	if [ -z "$dev" ]; then
		return 1
	fi

	# Take care of lilo boot arg, risky activating of all vg
	case "$dev" in
	fe[0-9]*)
		lvm vgchange -aly --ignorelockingfailure
		exit 0
		;;
	# FIXME: check major
	/dev/root)
		lvm vgchange -aly --ignorelockingfailure
		exit 0
		;;
	esac

	# Make sure that we have a d-m path
	dev="${dev#/dev/mapper/}"
	if [ "$dev" = "$1" ]; then
		return 1
	fi

	eval $(dmsetup splitname --nameprefixes --noheadings --rows "$dev")
        # Make sure that we have non-empty volume group and logical volume
        if [ -z "$DM_VG_NAME" ] || [ -z "$DM_LV_NAME" ]; then
                return 1
        fi
 
        # If the logical volume hasn't shown up yet, give it a little while
        # to deal with LVM on removable devices (inspired from scripts/local)
        fulldev="/dev/$DM_VG_NAME/$DM_LV_NAME"
        if [ -z "`lvm lvscan -a --ignorelockingfailure |grep $fulldev`" ]; then
                # Use default root delay
                slumber=$(( ${ROOTDELAY:-180} * 10 ))
                        /bin/sleep 5
        fi
 
        # Activate logical volume
        lvm lvchange -aly --ignorelockingfailure "$DM_VG_NAME/$DM_LV_NAME"
        rc=$?
        if [ $rc = 5 ]; then
                echo "Unable to find LVM volume $DM_VG_NAME/$DM_LV_NAME"

	fi
}

if [ ! -e /sbin/lvm ]; then
	exit 0
fi

modprobe -q dm-mod

activate_vg "$ROOT"
activate_vg "$resume"

exit 0
